# -*- coding: UTF-8 -*-
import json
import pymongo

from pymongo import MongoClient
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import time
import pandas as pd


def export_block(feature_name, peroid, csv_name='file.csv'):
    db = MongoClient('mongodb://39.104.227.148:27019/')['features']
    cursor = db[feature_name + '_' + peroid].find(
        {"time": {"$lte": "2019-02-20 03:00:00"}}).sort(
        [("time", pymongo.ASCENDING)])
    df = pd.DataFrame(list(cursor))
    print(df)
    df = df.get(['time', 'block_total_tx_count', 'block_total_value', 'block-hist-1', 'block-hist-2', 'block-hist-3',
                 'block-hist-4', 'block-hist-5', 'block-hist-6', 'block-hist-7', 'block-hist-8', 'block-hist-9',
                 'block-hist-10', 'block-hist-11'])
    df.to_csv('block_20180601_20190304-feature-%s.csv' % peroid, index=False)


if __name__ == '__main__':
    export_block('block', '15')
    export_block('block', '30')
    export_block('block', '60')
    pass
